<?php
/**
* PHP version 5
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package Textbook
* @subpackage User
* @author Aljaz Srebrnic <a2piratesoft@gmail.com>;
* @author E. Pavletič
* @copyright 2009 a2PirateSoftware
* @license GPL License v3 <http://www.gnu.org/copyleft/gpl.html>
*/

class Textbook_Form_AbstractTextbookForm extends ZNF_Action_ActionForm {

  /**
   * Metoda normalizeISBN
   *
   * Metoda normalizira niz ISBN, tako da iz niza izbriše vse znake, ki niso številke.
   * Edina izjema je zadnji znak v nizu, ki je laho črka X (predstavlja 'check digit' 10)
   *
   * @param string $isbn ISBN, ki ga je vnesel uporabnik
   * @return string Niz sestavljen iz samih ètevilk. Zadnji znak je lahko tudi 'X'. V primeru, da dolžina niza ni 10 (ISBN-10) ali 13 (ISBN-13), vrne null.
   * @access private
   */
  protected function normalizeISBN($isbn) {
    $isbn = trim($isbn);
    $normalized = '';

    for ($i=0; $i < strlen($isbn)-1; ++$i) {
      if (is_numeric($isbn{$i}))
	$normalized .= $isbn{$i};
    }
    // Preveri zadnji znak 
    if (!strcasecmp($isbn{strlen($isbn)-1}, 'X') || is_numeric($isbn{strlen($isbn)-1}))
      $normalized .= $isbn{strlen($isbn)-1};

    if ((strlen($normalized) != 10) && (strlen($normalized) != 13))
      $normalized = null;

    return $normalized;
  }
}